package com.hz.monitoring.module.system.controller.admin.media;

import cn.hutool.core.io.IoUtil;
import com.hz.monitoring.framework.common.exception.util.ServiceExceptionUtil;
import com.hz.monitoring.framework.common.pojo.CommonResult;
import com.hz.monitoring.module.infra.api.file.FileApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import static com.hz.monitoring.framework.common.pojo.CommonResult.success;
import static com.hz.monitoring.module.infra.enums.ErrorCodeConstants.FILE_IS_EMPTY;

@Api(tags = "管理后台 - 媒体")
@RestController
@RequestMapping("/system/media")
@Validated
@Slf4j
public class MediaController {
    @Resource
    private FileApi fileApi;

    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public CommonResult<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw ServiceExceptionUtil.exception(FILE_IS_EMPTY);
        }
        String avatar = fileApi.createFile(file.getBytes());
        return success(avatar);
    }

}
